package com.fc.push.handler.pub.verify;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.fc.persistence.dao.DAOTemplate;
import com.fc.push.GlobalServerConst;
import com.fc.push.db.TranscationFacade;
import com.fc.push.db.entities.Application;
import com.fc.push.handler.AbstractHttpHandler;
import com.fc.push.handler.HandlerResult;
import com.fc.push.handler.pub.MessageRCode;
import com.fc.remoting.anno.Protocol;

/**
 * 合法输入参数：
 * 必须参数：
 * 		module=1
 * 		order=5
 * 		下面两个参数用于查找应用
 * 		appname=xxx
 * 		platform=xxx
 * 
 * 下面是支持修改的应用属性，如果需要修改则添加：
 * 		pkcsfile=xxx
 * 		pwd=xxx
 * 		isproduction=xxx
 * @author liangh
 *
 */
@Component
@Protocol(module = VerifyConst.VERIFY_MODULE_ID, order = VerifyConst.ORDER_UPDATE_APPLICATION)
public class ApplicationUpdateHandler extends AbstractHttpHandler {
	private static final String PLATFORM = "platform";
	private static final String APPKEY = "appkey";
	private static final String APPNAME = "appname";
	private static final String PNAME = "pname";
	private static final String PKCSFILE = "pkcsfile";
	private static final String PWD = "pwd";
	private static final String IS_PRODUCTION = "isproduction";
	private static final String CODE = "code";
	@Override
	public Object handle(HttpServletResponse response, JSONObject request){
		String platform = request.getString(PLATFORM);
		String appkey = request.getString(APPKEY);
		
		if (platform == null || platform.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}
		
		platform = platform.toLowerCase();
		
		if (appkey == null || appkey.isEmpty()) {
			JSONObject object = HandlerResult.result();
			object.put(CODE, MessageRCode.RCODE_ERROR_1002);
			return object;
		}
				
		String pName = null;
		byte[] pkcsfile = null;
		String pwd = null;
		
		String sql = "from " + Application.class.getSimpleName() + " where appkey = '" + appkey + "'";
		List<DAOTemplate> resApp = TranscationFacade.getInstance().transcation().getBySQL(sql);
		if (resApp == null || resApp.size() == 0) {
			JSONObject result = HandlerResult.paramError();
			return result;
		}
		Application application = (Application) resApp.get(0);
		boolean isUpdate = false;
		if (platform.equals(GlobalServerConst.IOS)) {
			pkcsfile = request.getBytes(PKCSFILE);
			pwd = request.getString(PWD);
		} else if (platform.equals(GlobalServerConst.ANDROID)) {
			pName = request.getString(PNAME);
		}
		
		if(pkcsfile != null){
			isUpdate = true;
			application.setFile(pkcsfile);
		}
		if(pwd != null){
			isUpdate = true;
			application.setPwd(pwd);
		}
		if(pName != null && pName.equals("")){
			isUpdate = true;
			application.setPname(pName);
		}
		
		if(request.get(IS_PRODUCTION)!= null){
			int isProduction = request.getIntValue(IS_PRODUCTION);
			application.setIsproduction(isProduction);
			isUpdate = true;
		}
		
		JSONObject result = null;
		if(isUpdate){
			TranscationFacade.getInstance().transcation().save(application);
			result = HandlerResult.result();
			return result;
		}else{
			result = HandlerResult.paramError();
		}
		return result;
	}
}
